<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

/**
 * 前台文章控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class ArticleshowController extends HomeController {

    /**
     * 后台首页
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
        if(IS_GET){

            if(I('get.article_id') != null){
                $id = I('get.article_id');
            }else{
                $id = 0;
            }

            if(I('get.filter') != null){
                $filter = I('get.filter');
            }else{
                $filter = null;
            }

            if(I('get.search') != null){
                $search = I('get.search');
            }else{
                $search = null;
            }

            if(I('get.type') != null){
                $type = I('get.type');
            }else{
                $type =0;
            }

            if(I('get.page') != null){
                $page = I('get.page');
            }else{
                $page =1;
            }

            if(I('get.limit') != null){
                $limit = I('get.limit');
            }else{
                $limit =10;
            }

            $articleshowdata = $this-> articleShowGet($id,$type,$page,$limit,$order = 'create_time',$status = 1,$filter,$search);
            if($articleshowdata){
                $json['success'] = true;
                $json['info'] = $articleshowdata;
            }else{
                $json['success'] = false;
                $json['error'] = '文章找不到';
            }
            $this->ajaxReturn($json);
        }
    }

    public function articleShowGet($id,$type,$page,$limit,$order = 'create_time',$status = 1,$filter,$search){
        $uid = $this->login();
        $articleshowdata = $this->getArticleShow($id,$type,$page,$limit,$order = 'create_time',$status = 1,$filter,$search);
        $collectiondata = $this->collectionGet($uid);
        if($collectiondata['success']){
            $collectionArray = $collectiondata['info'];
        }else{
            $collectionArray = array();
        }
        foreach ($articleshowdata as $key => $value) {
            if(in_array($value['id'], $collectionArray)){
                $articleshowdata[$key]['collection'] = true;
            }else{
                $articleshowdata[$key]['collection'] = false;
            }
        }
        return $articleshowdata;
    }

    protected function getArticleShow($id,$type,$page,$limit,$order = 'create_time',$status = 1,$filter,$search){
        $articleshow = D('articleshow');
        $articleshowdata = $articleshow ->articleShowGet($id,$type,$page,$limit,$order = 'create_time',$status = 1,$filter,$search);
        
        foreach ($articleshowdata as $key => $value) {
             $save_data[] = array(
            'id' => $value['id'], 
            'title'=>$value['title'], 
            'content' => $value['content'], 
            'description'=>$value['description'], 
            'recommend' => (bool)$value['recommend'], 
            'hot' => (bool)$value['hot'], 
            'green' => (bool)$value['green'], 
            'party' => (bool)$value['party'], 
            'super' => (bool)$value['super'], 
            'hands' => (bool)$value['hands'], 
            // 'high_price'=>$value['high_price'], 
            // 'low_price' => $value['low_price'], 
            'one_price' =>floatval($value['one_price']),
            'two_price' => floatval($value['two_price']),
            'three_price'=>floatval($value['three_price']),
            'four_price' =>floatval($value['four_price']), 
            'collect_num'=>$value['collect_num'], 
            'collection' =>$value['collection'],
            'epense_explanation'=>$value['epense_explanation'],
            'renqun' => $value['renqun'],
            'zhuyi' => $value['zhuyi'],
            'shiyong' => $value['shiyong'],
            'tuigai' => $value['tuigai']
            );
            $banners = array();
            $banners =array(
                '0' => $this->imgIdToPicture($articleshowdata[$key]['banner1']), 
                '1' => $this->imgIdToPicture($articleshowdata[$key]['banner2']), 
                '2' => $this->imgIdToPicture($articleshowdata[$key]['banner3']), 
                '3' => $this->imgIdToPicture($articleshowdata[$key]['banner4']), 
                '4' => $this->imgIdToPicture($articleshowdata[$key]['banner5']), 
                );
            $save_banners_data =array();
            foreach ($banners as $key2 => $value2) {
                if($value2){
                    $save_banners_data[] = $value2;  
                }   
            }
            if($save_banners_data){
                $save_data[$key]['banners'] = $save_banners_data;
            }else{
                $save_data[$key]['banners'] = null;
            }
        }
        return $save_data;
    }

    protected function collectionGet($uid){
        $collection = D('collection');
        $collectiondata = $collection->collectionGet($uid);
        return $collectiondata;
    }

    public function imgIdToPicture($homepageimgdata){
        $picture = D('picture');

        $imgdata = $picture->getPictureByIds($homepageimgdata);
       
        return $imgdata;
    }

}
